import grpc
from protos_beta import detection_pb2, detection_pb2_grpc

import cv2
import numpy as np

def mat2bytes(image: np.ndarray) -> bytes:
    return np.array(cv2.imencode('.jpg', image)[1]).tobytes()

def stream_video(img, channel_n_port):
    with grpc.insecure_channel(channel_n_port) as channel:
        stub = detection_pb2_grpc.PartDetectionServerStub(channel)
        # img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

        img_bytes = mat2bytes(img)
        requests = detection_pb2.DetectFlawRequest(image = img_bytes, name='')
        try:
            response = stub.detectFlaw(requests)
        except:
            print('error!')